Survey.Survey.cssType = "bootstrap";
var surveyJSON = {
    locale: "zh-cn",
    pages: [{
            name: "page7",
            elements: [{
                    type: "html",
                    name: "question8",
                    html: {
                        "zh-cn": '<div class="container">\n\t\t<p class="lead"></p>\n\t\t\n\t\t<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！我们是湖南省脑卒中疾病医防融合试点项目工作组的调查员，这次调查由湖南省卫生健康委员会统一组织。主要是了解社区居民脑卒中患者发病率、患病率及病死率等相关信息，为国家制定卫生政策提供信息，希望得到您的配合。所调查的内容仅用于相关的分析，我们将按照中华人民共和国《统计法》的要求，对您回答的问题加以保密。希望您能够如实回答下面的问题，非常感谢您的合作！</p>\n\t\t<p style="color: red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本项目通过中南大学湘雅医院伦理委员会审核批准。</p><br>',
                    },
                },
                {
                    type: "html",
                    name: "question9",
                    html: {
                        "zh-cn": '<h5 style="color: red">您是否同意接受该调查：</h5>',
                    },
                },
                {
                    type: "checkbox",
                    name: "是否同意接受该调查",
                    title: {
                        "zh-cn": "是否同意接受该调查",
                    },
                    titleLocation: "hidden",
                    isRequired: true,
                    choices: ["同意"],
                },

                // 填写说明
                {
                    type: "html",
                    name: "question10",
                    visibleIf: '{是否同意接受该调查} = ["同意"]',
                    html: {
                        "zh-cn": '\t\t<p class="lead" >填写说明</p>\n\t\t\t\t<p style="color: red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;死亡患者仅录入2018年1月1日以后去世的</p>',
                    },
                },
                {
                    type: "text",
                    isRequired: true,
                    name: "填表人签名",
                    title: {
                        "zh-cn": "填表人签名"
                    },
                },
                {
                    type: "text",
                    name: "填表人联系电话",
                    title: {
                        "zh-cn": "联系电话"
                    },
                    "isRequired": true,
                    "validators": [{
                        "type": "text",
                        "minLength": 11,
                        "maxLength": 11,
                        "allowDigits": true,
                        "text": "电话号码长度应该为11"
                    }],
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "填表人填写日期",
                    title: {
                        "zh-cn": "填写日期"
                    },
                    inputType: "date"
                },
            ],
        },

        {
            name: "page8",
            elements: [{
                    type: "dropdown",
                    name: "所在的省",
                    isRequired: true,
                    title: "所在的省",
                    choices: ["湖南省"],
                },
                {
                    type: "dropdown",
                    name: "所在的市",
                    title: "所在的市",
                    isRequired: true,
                    choicesByUrl: {
                        url: "http://39.98.237.59:8085/{所在的省}",
                        valueName: "name",
                    },
                },
                {
                    type: "dropdown",
                    name: "所在的县或区",
                    title: "所在的县或区",
                    isRequired: true,
                    choicesByUrl: {
                        url: "http://39.98.237.59:8086/{所在的市}",
                        valueName: "name",
                    },
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "所在的乡镇或街道",
                    title: {
                        "zh-cn": "在的乡镇或街道",
                    },
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "所在的村或社区",
                    title: {
                        "zh-cn": "所在的村或社区",
                    },
                },
            ],
            title: {
                "zh-cn": "地址：",
            },
        },
        {
            name: "page9",
            elements: [{
                    type: "text",
                    isRequired: true,
                    name: "转送医院",
                    title: {
                        "zh-cn": "您所在社区脑卒中发病时常规转送医院名称：",
                    },
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "转送时间",
                    inputType: "number",
                    title: {
                        "zh-cn": "转送医院所需时间(分钟)",
                    },
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "转送距离",
                    inputType: "number",
                    title: {
                        "zh-cn": "转送医院距离(千米)",
                    },

                },

                {
                    type: "text",
                    isRequired: true,
                    name: "调查员签名",
                    title: {
                        "zh-cn": "调查员签名",
                    },
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "调查员联系电话",
                    title: {
                        "zh-cn": "调查员联系电话",
                    },
                    validators: [{
                        type: "text",
                        minLength: 8,
                        maxLength: 11,
                        allowDigits: true,
                        text: "手机号码长度应该为8或11位",
                    }, ],
                },
            ],
            title: {
                "zh-cn": "调查员登记：",
            },
        },
        {
            name: "page10",
            elements: [{
                    type: "text",
                    isRequired: true,
                    name: "个人编号",
                    title: {
                        "zh-cn": "个人编号",
                    },
                    readOnly: true
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "姓名",
                    title: {
                        "zh-cn": "姓名",
                    },
                },

                {
                    type: "text",
                    name: "年龄",
                    title: {
                        "zh-cn": "年龄（岁）",
                    },
                    isRequired: true,
                },

                {
                    type: "text",
                    name: "身高",
                    title: "身高（cm）",
                    isRequired: true,
                    inputType: "number",
                },

                {
                    type: "text",
                    name: "体重",
                    title: "体重（kg）",
                    isRequired: true,
                    inputType: "number",
                },

                {
                    type: "radiogroup",
                    name: "性别",
                    title: {
                        "zh-cn": "患者性别",
                    },
                    isRequired: true,
                    choices: ["男", "女"],
                },

                {
                    type: "radiogroup",
                    name: "婚姻状况",
                    title: "婚姻状况",
                    isRequired: true,
                    choices: ["未婚", "已婚", "离婚", "丧偶"],
                },

                {
                    type: "radiogroup",
                    name: "户籍人口",
                    title: {
                        "zh-cn": "户籍人口",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "radiogroup",
                    name: "常住人口",
                    title: {
                        "zh-cn": "常住人口",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "radiogroup",
                    name: "是否吸烟",
                    title: {
                        "zh-cn": "是否吸烟",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "radiogroup",
                    name: "是否饮酒",
                    title: {
                        "zh-cn": "是否饮酒",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "checkbox",
                    name: "特殊人群",
                    title: {
                        "zh-cn": "特殊人群",
                    },
                    isRequired: true,
                    choices: ["否", "高血压", "糖尿病", "高脂血症", "其他"],
                },

                {
                    type: "radiogroup",
                    name: "是否居住在家",
                    title: {
                        "zh-cn": "是否居住在家",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "radiogroup",
                    name: "是否中风",
                    title: {
                        "zh-cn": "是否中风",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                },

                {
                    type: "text",
                    isRequired: true,
                    visibleIf: '{是否中风} = "是"',
                    name: "中风日期",
                    title: {
                        "zh-cn": "发病年月"
                    },
                    inputType: "month"
                },

                {
                    type: "radiogroup",
                    name: "卒中类型",
                    visibleIf: '{是否中风} = "是"',
                    title: {
                        "zh-cn": "卒中类型",
                    },
                    isRequired: true,
                    choices: ["脑出血", "脑梗死"],
                },

                {
                    type: "radiogroup",
                    name: "是否生存",
                    visibleIf: '{是否中风} = "是"',
                    title: {
                        "zh-cn": "是否生存",
                    },
                    isRequired: true,
                    choices: ["否", "是"],
                },

                {
                    type: "radiogroup",
                    name: "死因",
                    visibleIf: '{是否生存} = "否"',
                    title: {
                        "zh-cn": "死因",
                    },
                    isRequired: true,
                    choices: ["脑出血", "脑梗死", "其他"],
                },
                {
                    type: "text",
                    name: "死亡日期",
                    title: {
                        "zh-cn": "死亡日期"
                    },
                    visibleIf: '{是否生存} = "否"',
                    isRequired: true,
                    "min": "2018-01-01",
                    inputType: "date",
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "手机号码",
                    visibleIf: '{是否中风} = "是"',
                    title: {
                        "zh-cn": "手机号码（填写脑中风或联系人号码）",
                    },
                    validators: [{
                        type: "text",
                        minLength: 11,
                        maxLength: 11,
                        allowDigits: true,
                        text: "手机号码长度应该为11",
                    }, ],
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "身份证号码",
                    visibleIf: '{是否中风} = "是"',
                    title: {
                        "zh-cn": "身份证号码",
                    },
                    validators: [{
                        type: "text",
                        text: "身份证字段长度应该为18",
                        minLength: 18,
                        maxLength: 18,
                        allowDigits: true,
                    }, ],
                },

                {
                    type: "text",
                    isRequired: true,
                    name: "登录账号",
                    title: "问卷id",
                    visible: false,
                    defaultValue: "1",
                },
                {
                    type: "text",
                    isRequired: true,
                    name: "_id",
                    title: "调查员id",
                    visible: false,
                    defaultValue: "11",
                },

                {
                    type: "html",
                    name: "是否还需再次编辑",
                    html: {
                        "zh-cn": '<h5 style="color: red">是否需要再次编辑:(注：如果问卷中填写的数据已经确定，请选择否，若还需要更改数据，请选择是)</h5>',
                    },
                },

                {
                    type: "radiogroup",
                    name: "是否需要再次编辑",
                    title: {
                        "zh-cn": "是否需要再次编辑",
                    },
                    isRequired: true,
                    choices: ["是", "否"],
                    titleLocation: "hidden",
                },
            ],
            title: {
                "zh-cn": "入户摸底全体居民情况登记：",
            },
        },
    ],
    clearInvisibleValues: "onHidden",
};
// 存储用户名和问卷ID
var username
var queID
window.onload = function () {
    var surveyData = $("#surveyData").text(); //将指定编辑的数据传过来
    survey.data = JSON.parse(surveyData); //转化为json格式,并赋值给survey的data
    console.log(survey.data);
    username = survey.data['登录账号']
    queID = survey.data['_id']
}
var survey = new Survey.Model(surveyJSON);
$("#surveyContainer").Survey({
    model: survey,
    onComplete: sendAllDataToServer
});

// 该函数实现填一个判断一个，判断的是有范围的，比如说电话号码长度
survey.checkErrorsMode = "onValueChanged";

survey.sendResultOnPageNext = true;
//用于添加问卷填入的自定义约束
survey.onValidateQuestion.add(function (s, options) {
    if (options.name == "身高") {
        if (options.value < 30 || options.value > 300)
            options.error = "身高范围应在30cm到300cm之间"
    } else if (options.name == "体重") {
        if (options.value < 10 || options.value > 250)
            options.error = "体重范围应在10kg到250kg之间"
    }
});



function sendAllDataToServer(survey) {
    /* 此处需要手动复制，可能是因为只能提交没有隐藏的数据 */
    queData = survey.data
    queData['登录账号'] = username
    queData['_id'] = queID
    $.ajax({
        url: '/pcpages/submitEditQue7/',
        type: 'post',
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(queData),
        success: function (data) {
            console.log('数据录入成功');
            window.location.href="/pcpages/pcpages_cgx/no7/"
        }
    });
}
